package cn.bughub.service.impl;

import cn.bughub.mapper.CaptchaMapper;
import cn.bughub.model.entity.CaptchaEntity;
import cn.bughub.service.CaptchaService;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.GifCaptcha;
import cn.hutool.core.lang.UUID;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 验证码业务实现类
 *
 * @author zwj
 * @date 2024-09-24
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CaptchaServiceImpl implements CaptchaService {
    
    private final CaptchaMapper captchaMapper;
    
    private final HttpServletResponse response;
    /**
     * 获取验证码
     */
    @Override
    public void generateCaptcha() throws IOException {
        
        GifCaptcha gifCaptcha = CaptchaUtil.createGifCaptcha(80, 40, 4);
        String code = gifCaptcha.getCode();
        CaptchaEntity captchaEntity = new CaptchaEntity();
        captchaEntity.setUuid(UUID.fastUUID().toString());
        captchaEntity.setCaptchaResult(code);
        captchaMapper.insert(captchaEntity);
        gifCaptcha.write(response.getOutputStream());
    }
    
}
